package com.cnhaimei.vod.logic;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.cnhaimei.base.BaseActivity;
import com.cnhaimei.vod.R;

public class HelpActivity extends BaseActivity implements OnGestureListener{

	private GestureDetector mDetector;
	private ViewFlipper mHelpFlipper;
	private View mHelpLayout;
	private int mHelpFlipperIndex = 0;
	private int mHelpImgCount = 4;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v8_help);
		initView();
	}

	//设置标题
	protected void setTitle(String title)
	{
		((TextView)findViewById(R.id.title)).setText(title);
	}
	
	private void initView() {
		setTitle("帮助");
		
		mHelpLayout = (View) findViewById(R.id.help_layout);
		mHelpFlipper = (ViewFlipper)findViewById(R.id.help_viewflipper);
		mDetector = new GestureDetector(this);
		showHelp(true);

		setClickable(R.id.back);
		findViewById(R.id.back).setVisibility(View.VISIBLE);
	}

	private void toggleTitleBar()
	{
		View view = findViewById(R.id.head);
		view.setVisibility(view.getVisibility() == View.INVISIBLE ? View.VISIBLE : View.INVISIBLE);
	}
	
	private void showHelp(boolean show) {
		mHelpLayout.setVisibility(show ? View.VISIBLE : View.GONE);
	}

	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.mDetector.onTouchEvent(event);
	}

	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float arg2,
			float arg3) {
		Log.i("Fling", "Fling Happened!");
		if (e1.getX() - e2.getX() > 5) {
			this.mHelpFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_out));
			this.mHelpFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			if (mHelpFlipperIndex < mHelpImgCount - 1) {
				this.mHelpFlipper.showNext();
				mHelpFlipperIndex++;
				//TODO:若有点号索引，则可在此处理
			}
			else {//达到最后一页，在翻页则退出。				
				//finish();
			}

			return true;
		} else if (e1.getX() - e2.getX() < -5) {
			this.mHelpFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			this.mHelpFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_out));
			if (mHelpFlipperIndex > 0) {
				this.mHelpFlipper.showPrevious();
				mHelpFlipperIndex--;
				//TODO:若有点号索引，则可在此处理
			}
			return true;
		}
		return true;
	}

	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		toggleTitleBar();
		return false;
	}
	
    public void drawHelpBackground()
    {
    	GradientDrawable grad = new GradientDrawable( 
    			   Orientation.TL_BR,
    			   new int[] {Color.rgb(0, 0, 127),
    					   Color.rgb(0, 0, 255),
    					   Color.rgb(127, 0, 255),
    					   Color.rgb(127, 127, 255),
    					   Color.rgb(127, 255, 255),
    					   Color.rgb(255, 255, 255)} 
    	); 

    	mHelpLayout.setBackgroundDrawable(grad);
    }

}
